********Merge lifetime variables and clean MXFLS data for analysis********

args mxfls45

use "`mxfls45'", clear

drop if mxfls == .
drop if pid_link == .
sort pid_link mxfls

duplicates drop pid_link mxfls, force

*Merge lifetime macroeconomic experiences
cd "$reploc/data/created_data"
merge 1:1 pid_link mxfls using indiv_provgrbir_stats_mex.dta, gen(_mergegrowth)

*Merge lifetime macroeconomic experiences, from state of residence
cd "$reploc/data/created_data"
merge 1:1 pid_link mxfls using indiv_provgrres_stats_mex.dta, gen(_mergegrowthlife)

forval lambda=-0(10)0 {
local lam100=round(`lambda')

if `lambda'<0 {
local hola=substr("`lam100'",2,.)
local lamnam neg`hola'
}
else {
local lamnam `lam100'
}

local provavgs `provavgs' grbrprovAvg_lam`lamnam' grbrprovSD_lam`lamnam' grexprovAvg_lam`lamnam' grexprovSD_lam`lamnam'
}

foreach v in `provavgs' {
gen `v' = .
replace `v' = `v'_41mx2 if mxfls == 2
replace `v' = `v'_41mx3 if mxfls == 3

gen `v'co2 = .
replace `v'co2 = `v'_41mx2co2 if mxfls == 2
replace `v'co2 = `v'_41mx3co2 if mxfls == 3

gen `v'co3 = .
replace `v'co3 = `v'_41mx2co3 if mxfls == 2
replace `v'co3 = `v'_41mx3co3 if mxfls == 3
}

foreach v in `provavgs' {

replace `v' = . if birthyr<1925 // as otherwise growth variables are not properly constructed
replace `v'co2  = . if birthyr<1925 // as otherwise growth variables are not properly constructed
replace `v'co3  = . if birthyr<1925 // as otherwise growth variables are not properly constructed

}

*Demographics
gen age=.
replace age=mxfls2_edad if mxfls==2
replace age=mxfls3_edad if mxfls==3
la var age "Age"

gen imp_age = .
replace imp_age = yearinterviewmx2 - birthyr if mxfls == 2
replace imp_age = yearinterviewmx3 - birthyr if mxfls == 3
drop if imp_age <= 15

gen imp_age_sq = .
replace imp_age_sq = imp_age^2

gen sex=.
replace sex=mxfls2_ls04 if mxfls==2
replace sex=mxfls3_ls04 if mxfls==3

gen woman = .
replace woman = 1 if sex == 3
replace woman = 0 if sex == 1

gen  yearinterview=.
replace yearinterview=yearinterviewmx2 if mxfls==2
replace yearinterview=yearinterviewmx3 if mxfls==3

gen monthinterview=.
replace monthinterview=mxfls2_mes if mxfls==2
replace monthinterview=mxfls3_mes if mxfls==3


gen mxfls2_marstat=mxfls2_edo_civil if mxfls==2
gen mxfls3_marstat= mxfls3_edo_civil if mxfls==3

gen marstat=.
replace marstat=mxfls2_marstat if mxfls==2
replace marstat=mxfls3_marstat if mxfls==3

gen married = .
replace married = 0 if (marstat==1 | marstat==2 | marstat==4 | marstat==5)  & marstat!=.
replace married = 1 if (marstat==3 | marstat==6) & marstat!=.
la var married "Married"

gen educlevel=.
replace educlevel=mxfls2_ls14 if mxfls==2
replace educlevel=mxfls3_ls14 if mxfls==3

gen edlevel=.
replace edlevel = 1 if educlevel == 1 | educlevel == 2 | educlevel==3
replace edlevel = 2 if educlevel == 4 | educlevel == 5 
replace edlevel = 3 if educlevel == 6 | educlevel == 7
replace edlevel = 4 if educlevel == 8 | educlevel == 9 | educlevel == 10
la var edlevel "Highest education level attained (1 elem, 2 jun, 3 high, 4 coll)"

tab edlevel, gen (Eddum)

la var Eddum1 "Comp. Elementary"
la var Eddum2 "Comp. Junior High"
la var Eddum3 "Comp. High School"
la var Eddum4 "Above High School"

gen hhsize = .
replace hhsize = mxfls2_hhsize if mxfls==2
replace hhsize = mxfls3_hhsize if mxfls==3
la var hhsize "Household Size"

gen hhsize_sq=.
replace hhsize_sq = mxfls2_hhsize^2 if mxfls==2
replace hhsize_sq = mxfls3_hhsize^2 if mxfls==3
la var hhsize_sq "Household Size Squared"

*Work information
gen workcat=.
replace workcat=mxfls2_tb32p if mxfls==2 
replace workcat=mxfls3_tb32p if mxfls==3 

gen entrep=.
replace entrep = 1 if workcat==5 | workcat==6
replace entrep = 0 if workcat==1 | workcat==2 | workcat==3 | workcat==4 | workcat==7
la var entrep "Entrepreneur (self-employed or business owner)"

gen employed=.
replace employed=1 if (mxfls2_tb03==1 | mxfls2_tb04==1 | mxfls2_tb05==1) & mxfls==2
replace employed=0 if (mxfls2_tb03==3 & mxfls2_tb04==3 & mxfls2_tb05==3) & mxfls==2

replace employed=1 if (mxfls3_tb03==1 | mxfls3_tb04==1 | mxfls3_tb05==1) & mxfls==3
replace employed=0 if (mxfls3_tb03==3 & mxfls3_tb04==3 & mxfls3_tb05==3) & mxfls==3

gen unemployed=.
replace unemployed=1 if (employed==0 & mxfls2_tb02_1==2) & mxfls==2
replace unemployed=0 if (employed==1 | mxfls2_tb02_1!=2) & mxfls==2
replace unemployed=1 if (employed==0 & mxfls3_tb02_1==2) & mxfls==3
replace unemployed=0 if (employed==1 | mxfls3_tb02_1!=2) & mxfls==3

gen workcat_an=.
replace workcat_an=1 if workcat==5 | workcat==6
replace workcat_an=2 if workcat==3 
replace workcat_an=4 if workcat==2
replace workcat_an=5 if workcat==4
replace workcat_an=7 if workcat==7
replace workcat_an=8 if workcat==1

/*
1. Peasant on your plot
2. Family worker in a household-owned business, without remuneration CH
3. Non-agricultural worker or employee CH
4. Rural laborer, or land peon (agricultural worker) ch
5. Boss, employer, or business owner CH
6. Self-employed worker
7. Worker without remuneration of a business or company that is not owned by the household
*/

*Industry only asked in MxFLS2
gen industry=.
replace industry=1 if mxfls==2 & mxfls2_tb24_26p_scian==11
replace industry=2 if mxfls==2 & mxfls2_tb24_26p_scian==21
replace industry=3 if mxfls==2 & mxfls2_tb24_26p_scian==31 | mxfls2_tb24_26p_scian==32 | mxfls2_tb24_26p_scian==33
replace industry=4 if mxfls==2 & mxfls2_tb24_26p_scian==22
replace industry=5 if mxfls==2 & mxfls2_tb24_26p_scian==23
replace industry=6 if mxfls==2 & mxfls2_tb24_26p_scian==43 | mxfls2_tb24_26p_scian==46 | mxfls2_tb24_26p_scian==71 | mxfls2_tb24_26p_scian==72
replace industry=7 if mxfls==2 & mxfls2_tb24_26p_scian==48 | mxfls2_tb24_26p_scian==49 | mxfls2_tb24_26p_scian==51
replace industry=8 if mxfls==2 & mxfls2_tb24_26p_scian==52 | mxfls2_tb24_26p_scian==53 | mxfls2_tb24_26p_scian==54 | mxfls2_tb24_26p_scian==56
replace industry=9 if mxfls==2 & mxfls2_tb24_26p_scian==61 | mxfls2_tb24_26p_scian==62
replace industry=10 if mxfls==2 & mxfls2_tb24_26p_scian==81 | mxfls2_tb24_26p_scian==93 | mxfls2_tb24_26p_scian==99

/*
CODE TK19Ab INDO - WITH MEX USED NAICS
Agriculture, forestry, fishing and hunting	01
Mining and quarrying	02
Manufacturing	03
Electricity, gas, water	04
Construction	05 	
Wholesale, retail, restaurants and hotels	06
Transportation, storage and communications	07
Finance, insurance, real estate and business services	08
Social services	09
Activities that cannot be classified	10  
*/

*Smoking

gen eversmoke2 = .
replace eversmoke2 = 0 if mxfls2_gh06==3 & mxfls == 2
replace eversmoke2 = 1 if  mxfls2_gh06==1 & mxfls == 2

gen eversmoke3 = .
replace eversmoke3 = 0 if mxfls3_gh06==3 & mxfls == 3
replace eversmoke3 = 1 if  mxfls3_gh06==1 & mxfls == 3

gen eversmoke=.
replace eversmoke=eversmoke2 if mxfls==2
replace eversmoke=eversmoke3 if mxfls==3

gen smoke2 = .
replace smoke2 = 1 if eversmoke==1 & mxfls2_gh09_1==9 & mxfls == 2
replace smoke2 = 0 if eversmoke==0 | mxfls2_gh09_1==1 | mxfls2_gh09_1==2 & mxfls == 2

gen smoke3 = .
replace smoke3 = 1 if eversmoke==1 & mxfls3_gh09_1==9 & mxfls == 3
replace smoke3 = 0 if eversmoke==0 | mxfls3_gh09_1==1 | mxfls3_gh09_1==2 & mxfls == 3

gen smoke=.
replace smoke=smoke2 if mxfls==2
replace smoke=smoke3 if mxfls==3
la var smoke "Currently smoke"

drop smoke?

gen smoketot = .
replace smoketot=mxfls2_gh10_21 if mxfls==2 & smoke==1 & mxfls2_gh10_1==1
replace smoketot=mxfls3_gh10_21 if mxfls==3 & smoke==1 & mxfls3_gh10_1==1

*In packs
replace smoketot=mxfls2_gh10_22*20 if mxfls==2 & smoke==1 & mxfls2_gh10_1==2
replace smoketot=mxfls3_gh10_22*20 if mxfls==3 & smoke==1 & mxfls3_gh10_1==2
replace smoketot = 0 if smoke == 0
la var smoketot "# cigs/cigars smoked per day"

*Natural disaster and violence experiences
gen fear_assaultday=.
replace fear_assaultday=mxfls2_vli01 if mxfls==2
replace fear_assaultday=mxfls3_vli01 if mxfls==3
la var fear_assaultday "Scared of being assaulted during day"

gen fear_assaultnight=.
replace fear_assaultnight=mxfls2_vli02 if mxfls==2
replace fear_assaultnight=mxfls3_vli02 if mxfls==3
la var fear_assaultnight "Scared of being assaulted during night"

gen times_assaulted=0
replace times_assaulted=mxfls2_vli06 if mxfls==2 & mxfls2_vli05==1
replace times_assaulted=mxfls3_vli06 if mxfls==3 & mxfls3_vli05==1
la var times_assaulted "Times assaulted"

gen feelsafehome=.
replace feelsafehome=mxfls2_vlh04 if mxfls==2
replace feelsafehome=mxfls3_vlh04 if mxfls==3
la var feelsafehome "Feel safe at home (1 very safe, 4 very unsafe)"

gen famfriendrobbed=0
replace famfriendrobbed=mxfls2_vlh11a if mxfls==2 & mxfls2_vlh10a==1
replace famfriendrobbed=mxfls3_vlh11a if mxfls==3 & mxfls3_vlh10a==1
la var famfriendrobbed "Number fam/friend robbed in last 12 months"

gen famfriendassault=0
replace famfriendassault=mxfls2_vlh11b if mxfls==2 & mxfls2_vlh10b==1
replace famfriendassault=mxfls3_vlh11b if mxfls==3 & mxfls3_vlh10b==1
la var famfriendassault "Number fam/friend assaulted in last 12 months"

gen famfriendkidnapped=0
replace famfriendkidnapped=mxfls2_vlh11c if mxfls==2 & mxfls2_vlh10c==1
replace famfriendkidnapped=mxfls3_vlh11c if mxfls==3 & mxfls3_vlh10c==1
la var famfriendkidnapped "Number fam/friend kidnapped in last 12 months"

egen hhviol = rowtotal (famfriendrobbed famfriendassault famfriendkidnapped)
la var hhviol "Experienced hh violenced (# fam/friend robbed, assaulted, kidnapped) last 12 months"

gen disaster=0
replace disaster=1 if mxfls==2 & mxfls2_se01d==1
replace disaster=1 if mxfls==3 & mxfls3_se01d==1
la var famfriendkidnapped "House/business lost due to natural disaster"

*Migration
cd "$reploc/data/created_data"
merge 1:1 pid_link mxfls using indiv_prov_hist_mexico, gen(_mergmig) keepus(migrate earliestyrmig)

gen evermig = .
replace evermig = migrate 
la var evermig "Migrated cross prov lines in past"

*Earliest year of migration
gen earliestyrmigTOT1=earliestyrmig if mxfls==2
replace earliestyrmigTOT1=earliestyrmig if mxfls==3
egen earliestyrmigTOT=min(earliestyrmigTOT1), by(pid_link)
drop earliestyrmigTOT1

*HH economic variables

*Income
gen income=.
replace income=mxfls2_totinc if mxfls==2
replace income=mxfls3_totinc if mxfls==3
la var income "Household Income"

*Assets
gen assets=.
replace assets=mxfls2_ahvaltot if mxfls==2
replace assets=mxfls3_ahvaltot if mxfls==3
la var assets "Household Assets"

*Borrowing
gen borrowingtotal=.
replace borrowingtotal=mxfls2_totbor if mxfls==2
replace borrowingtotal=mxfls3_totbor if mxfls==3
la var borrowingtotal "Household borrowing"

*Savings
gen savingtotal=.
replace savingtotal=mxfls2_totsav if mxfls==2
replace savingtotal=mxfls3_totsav if mxfls==3
la var savingtotal "Household savings"

*Net Yearly Savings (savings - borrowing)
gen savingnettotal=.
replace savingnettotal=mxfls2_totsav-mxfls2_totbor if mxfls==2
replace savingnettotal=mxfls3_totsav-mxfls3_totbor if mxfls==3
la var savingnettotal "Household net savings (-borrowing)"

*Consumption
gen aggexpcons=.
replace aggexpcons=mxfls2_totcons if mxfls==2
replace aggexpcons=mxfls3_totcons if mxfls==3
la var aggexpcons "Household Consumption"

*Liquid assets
gen liquidassets=.
ereplace liquidassets=rowtotal(mxfls2_hhasssav mxfls2_hhassliv mxfls2_hhasspoul) if mxfls==2
ereplace liquidassets=rowtotal(mxfls3_hhasssav mxfls3_hhassliv mxfls3_hhasspoul) if mxfls==3
la var liquidassets "Household Liquid Assets"

*Non-liquid assets
gen nonliquidassets=.
replace nonliquidassets=assets-liquidassets
la var nonliquidassets "Household Non-Liquid Assets"

*House + land assets
gen homelandassets=.
ereplace homelandassets=rowtotal(mxfls2_hhasshouseland mxfls2_hhassothhouseland) if mxfls==2
ereplace homelandassets=rowtotal(mxfls3_hhasshouseland mxfls3_hhassothhouseland) if mxfls==3

*Non-House or land Assets
gen nonhomelandassets=assets-homelandassets

*Labor income
gen laborincome=.
replace laborincome=mxfls2_totallaborincome if mxfls==2
replace laborincome=mxfls3_totallaborincome if mxfls==3

*Non-Labor income
gen nonlaborincome=income-laborincome

*Inflation adjustment
gen ent=.
replace ent=mxfls2_ent if mxfls==2
replace ent=mxfls3_ent if mxfls==3
label variable ent "State where surveyed"

gen municipio=.
replace municipio=mxfls2_mpio if mxfls==2
replace municipio=mxfls3_mpio if mxfls==3
label variable ent "Municipality where surveyed"

gen region=.
replace region=1 if ent==2 | ent==3 | ent==28
replace region=2 if ent==18 | ent==25 | ent==26 
replace region=3 if ent==5 | ent==8 | ent==10 | ent==19 | ent==32
replace region=4 if ent==1 | ent==6 | ent==11 | ent==14 | ent==16 | ent==22 | ent==24
replace region=5 if ent==15 | ent==12 | ent==13 | ent==17 | ent==21 | ent==29 | ent==30
replace region=6 if ent==4 | ent==7 | ent==20 | ent==31 | ent==23 | ent==27 
replace region=7 if ent==9

cd "$reploc/data"
merge m:1 region using Mex_prov_cpi.dta, gen(_mergeregion)

gen cpimxfls2=.
gen cpimxfls3=.
forval yr=2004/2012{
replace cpimxfls2=cpi12_`yr' if yearinterviewmx2==`yr'
replace cpimxfls3=cpi12_`yr' if yearinterviewmx3==`yr'
}

egen cpimxfls2tot=min(cpimxfls2), by(pid_link)
egen cpimxfls3tot=min(cpimxfls3), by(pid_link)

local monvar income assets borrowingtotal savingtotal savingnettotal aggexpcons liquidassets nonliquidassets homelandassets nonhomelandassets laborincome nonlaborincome

foreach x in `monvar' {
	gen `x'_ia = .
	replace `x'_ia = `x'*cpi12_2005/cpimxfls2 if mxfls == 2
	replace `x'_ia = `x'*cpi12_2005/cpimxfls3 if mxfls == 3
	la var `x'_ia "Inflation adjusted `x'"
}

*Per-capita adjustment
foreach x in `monvar'{
	gen `x'_pc = .
	replace `x'_pc = `x'
	replace `x'_pc = `x'/hhsize if hhsize != .
	la var `x'_pc "Per capita `x'"
}

*Monthly Income
gen mth_inc = income/12
gen mth_inc_ia = income_ia/12

gen inflation = .
replace inflation = (cpimxfls3tot)/cpimxfls2tot if mxfls==3
replace inflation = 1 if mxfls==2
la var inflation "Inflation rate mxfls2-mxfls3"

*Risk preferences
gen mxfls2_buc1 = 0
replace mxfls2_buc1 = 1 if (mxfls2_rg02 == 2 & mxfls2_rg03 == 2 & mxfls2_rg04 == 2 & mxfls2_rg05 ==. & mxfls2_rg06 ==. & mxfls2_rg07 ==.)
gen mxfls2_buc2 = 0
replace mxfls2_buc2 = 1 if (mxfls2_rg02 == 2 & mxfls2_rg03 ==2 & mxfls2_rg04 ==1 & mxfls2_rg05 ==. & mxfls2_rg06 ==. & mxfls2_rg07 ==.) 
gen mxfls2_buc3 = 0
replace mxfls2_buc3 = 1 if (mxfls2_rg02 == 2 & mxfls2_rg03 ==1 & mxfls2_rg04 ==. & mxfls2_rg05 ==2 & mxfls2_rg06 ==. & mxfls2_rg07 ==.)
gen mxfls2_buc4 = 0
replace mxfls2_buc4 = 1 if (mxfls2_rg02 == 1 & mxfls2_rg03 ==. & mxfls2_rg04 ==. & mxfls2_rg05 ==2 & mxfls2_rg06 ==. & mxfls2_rg07 ==.)
gen mxfls2_buc5 = 0
replace mxfls2_buc5 = 1 if (mxfls2_rg02 == 1 & mxfls2_rg03 ==. & mxfls2_rg04 ==. & mxfls2_rg05 ==1 & mxfls2_rg06 ==2 & mxfls2_rg07 ==.) | (mxfls2_rg02 == 2 & mxfls2_rg03 ==1 & mxfls2_rg04 ==. & mxfls2_rg05 ==1 & mxfls2_rg06 ==2 & mxfls2_rg07 ==.)
gen mxfls2_buc6 = 0
replace mxfls2_buc6 = 1 if (mxfls2_rg02 == 1 & mxfls2_rg03 ==. & mxfls2_rg04 ==. & mxfls2_rg05 ==1 & mxfls2_rg06 ==1 & mxfls2_rg07 ==2) | (mxfls2_rg02 == 2 & mxfls2_rg03 ==1 & mxfls2_rg04 ==. & mxfls2_rg05 ==1 & mxfls2_rg06 ==1 & mxfls2_rg07 ==2)
gen mxfls2_buc7 = 0
replace mxfls2_buc7 = 1 if (mxfls2_rg02 == 1 & mxfls2_rg03 ==. & mxfls2_rg04 ==. & mxfls2_rg05 ==1 & mxfls2_rg06 ==1 & mxfls2_rg07 ==1) | (mxfls2_rg02 == 2 & mxfls2_rg03 ==1 & mxfls2_rg04 ==. & mxfls2_rg05 ==1 & mxfls2_rg06 ==1 & mxfls2_rg07 ==1)

foreach var of varlist mxfls2_buc* {
replace `var'=. if (mxfls2_rg02 ==8 | mxfls2_rg03 ==8 | mxfls2_rg04 ==8 | mxfls2_rg05==8 | mxfls2_rg06 ==8 | mxfls2_rg07 ==8)
}

egen hol2=rowtotal(mxfls2_buc*)
foreach var of varlist mxfls2_buc* {
replace `var'=. if hol2==0
}

gen mxfls2_bucweird=0
replace mxfls2_bucweird=1 if (mxfls2_rg02 == 2 & mxfls2_rg03 ==1 & mxfls2_rg04 ==. & mxfls2_rg05 ==1)



gen mxfls3_buc1 = 0
replace mxfls3_buc1 = 1 if (((mxfls3_rg01==2 & mxfls3_rg02==.) | (mxfls3_rg01==1 & mxfls3_rg02 ==2)) & mxfls3_rg03==2 &  mxfls3_rg04==2 & mxfls3_rg05==2 & mxfls3_rg06==2)
gen mxfls3_buc2 = 0
replace mxfls3_buc2 = 1 if (((mxfls3_rg01==2 & mxfls3_rg02==.) | (mxfls3_rg01==1 & mxfls3_rg02 ==2)) & mxfls3_rg03==2 & mxfls3_rg04==2 & mxfls3_rg05==2 & mxfls3_rg06==1)
gen mxfls3_buc3 = 0
replace mxfls3_buc3 = 1 if (((mxfls3_rg01==2 & mxfls3_rg02==.) | (mxfls3_rg01==1 & mxfls3_rg02 ==2)) & mxfls3_rg03==2 & mxfls3_rg04==2 & mxfls3_rg05==1)
gen mxfls3_buc4 = 0
replace mxfls3_buc4 = 1 if (((mxfls3_rg01==2 & mxfls3_rg02==.) | (mxfls3_rg01==1 & mxfls3_rg02 ==2)) & mxfls3_rg03==2 & mxfls3_rg04==1)
gen mxfls3_buc5 = 0
replace mxfls3_buc5 = 1 if (((mxfls3_rg01==2 & mxfls3_rg02==.) | (mxfls3_rg01==1 & mxfls3_rg02 ==2)) & mxfls3_rg03==1)
gen mxfls3_buc6 = 0
replace mxfls3_buc6 = 1 if (mxfls3_rg01 == 1 & mxfls3_rg02 ==1 & mxfls3_rg07 ==2)
gen mxfls3_buc7 = 0
replace mxfls3_buc7 = 1 if (mxfls3_rg01 == 1 & mxfls3_rg02 ==1 & mxfls3_rg07 ==1)

egen hol3=rowtotal(mxfls3_buc*)
foreach var of varlist mxfls3_buc* {
replace `var'=. if hol3==0
}

drop if hol2 == 0 & hol3 == 0

gen mxfls3_rbuc = 0
replace mxfls3_rbuc = 1 if mxfls3_buc1 == 1
replace mxfls3_rbuc = 2 if mxfls3_buc2 == 1
replace mxfls3_rbuc = 3 if mxfls3_buc3 == 1
replace mxfls3_rbuc = 4 if mxfls3_buc4 == 1
replace mxfls3_rbuc = 5 if mxfls3_buc5 == 1
replace mxfls3_rbuc = 6 if mxfls3_buc6 == 1
replace mxfls3_rbuc = 7 if mxfls3_buc7 == 1

gen mxfls2_rbuc = 0
replace mxfls2_rbuc = 1 if mxfls2_buc1 == 1
replace mxfls2_rbuc = 2 if mxfls2_buc2 == 1
replace mxfls2_rbuc = 3 if mxfls2_buc3 == 1
replace mxfls2_rbuc = 4 if mxfls2_buc4 == 1
replace mxfls2_rbuc = 5 if mxfls2_buc5 == 1
replace mxfls2_rbuc = 6 if mxfls2_buc6 == 1
replace mxfls2_rbuc = 7 if mxfls2_buc7 == 1

label variable mxfls3_rbuc "risk bucket mxfls3"
label variable mxfls2_rbuc "risk bucket mxfls2"

gen rbucFULL=.
replace rbucFULL=mxfls2_rbuc if mxfls==2
replace rbucFULL=mxfls3_rbuc if mxfls==3
label var rbucFULL "Risk aversion buckets, no collapsing for mxfls2"

gen rbuc=.
replace rbuc=mxfls3_rbuc if mxfls==3
replace rbuc=mxfls2_rbuc if mxfls==2 & mxfls2_rbuc<=5
replace rbuc=5 if mxfls==2 & (mxfls2_rbuc==6 | mxfls2_rbuc==7)
label var rbuc "Risk aversion buckets, with collapsing for mxfls2"

gen rbuc_nga = .
replace rbuc_nga = rbuc if (rbuc<6 & mxfls==3) | mxfls==2

gen rbucFULL_nga = .
replace rbucFULL_nga = rbucFULL if (rbucFULL<6 & mxfls==3) | mxfls==2

gen gambleaverse=.
replace gambleaverse=1 if rbuc>=6 & mxfls==3
replace gambleaverse=0 if rbuc<6 | mxfls==2

forval i=2/3 {
gen b3bne`i'=.
replace b3bne`i'=mxfls2_b3bne`i' if mxfls==2
replace b3bne`i'=mxfls3_b3bne`i' if mxfls==3
}

gen totcogscore=.
replace totcogscore=mxfls2_totcogscore if mxfls==2
replace totcogscore=mxfls3_totcogscore if mxfls==3

gen rbucgaref2a=rbuc if (b3bne3<=2 | gambleaverse==0 | b3bne3==.)
gen rbucgaref3a=rbuc if (totcogscore>1 | gambleaverse==0 | totcogscore==.)
gen rbuc_garefined=rbuc if rbucgaref2a!=. & rbucgaref3a!=.
gen rbucgaref2aFULL=rbucFULL if (b3bne3<=2 | gambleaverse==0 | b3bne3==.)
gen rbucgaref3aFULL=rbucFULL if (totcogscore>1 | gambleaverse==0 | totcogscore==.)
gen rbuc_garefinedFULL=rbucFULL if rbucgaref2aFULL!=. & rbucgaref3aFULL!=.

*Binarized measure of risk aversion
gen riskaverse = .
replace riskaverse = 1 if rbucFULL == 3 | rbucFULL == 4 | rbucFULL==5 | rbucFULL==6 | rbucFULL==7
replace riskaverse = 0 if rbucFULL == 1 | rbucFULL==2
la var riskaverse "Risk Averse"

*Merge with homicide rate variable from Brown et al
cd "$reploc/data"
sort pid_link mxfls
gen entmxfls2=ent if mxfls==2
replace entmxfls2=entmxfls2[_n-1] if pid_link==pid_link[_n-1] & mxfls==3

gen municipiomxfls2=municipio if mxfls==2
replace municipiomxfls2=municipiomxfls2[_n-1] if pid_link==pid_link[_n-1] & mxfls==3



merge m:1 entmxfls2 municipiomxfls2 mxfls yearinterview monthinterview using Mex_munic_homicideBROWNETAL.dta, gen(_mergebrown) keep(1 3)

tempfile dataintmex
save "`dataintmex'"


*Structural estimation

import excel "$reploc/data/Mex_bounds", clear first ///Matlab code to construct these bounds available upon request

*Put + and - infinity in right format
foreach var of varlist * {
local hola=substr("`var'",-4,2)
if "`hola'"=="UB" {
replace `var'=9999999 if `var'==65535
}

if "`hola'"=="LB" {
replace `var'=-9999999 if `var'==65535
}

}

capture drop _mergeSTRUCEST 
merge 1:1 pid_link mxfls using "`dataintmex'", gen(_mergeSTRUCEST)

local xvars income_pc_monthly NarrowBracket

foreach x in `xvars' {
gen `x'normUB=`x'_UB_A 
gen `x'normLB=`x'_LB_A 

replace `x'_UB_A=. if `x'normUB<`x'normLB
replace `x'_LB_A=. if `x'normUB<`x'normLB
}

drop *normUB *normLB



*Sample selection
drop if mxfls == .
drop if pid_link == .
drop if birthyr == . | birthyr==0

sort pid_link mxfls
quietly by pid_link: gen panel = cond(_N==1,0,_n)
replace panel = 1 if panel == 2
la var panel "appear in both waves"

*Birth province by cohort
egen yearprov = group(birthstate birthyr) 

